/**
 * 一些常用的基础方法
 * whetherNavigate 登录后跳转判断
 * unixToDate    将unix时间戳转换为指定格式
 * dateToUnix    将时间转unix时间戳
 * deepClone     对一个对象进行深拷贝
 * formatPrice   货币格式化
 * secrecyMobile 手机号隐私保护
 * randomString  随机生成指定长度的字符串
 */

/**
 * 验证银行卡号
 */
export function checkbankNo(bankNo: string): boolean {
  const bankNoString: string = bankNo.toString()
  const lastNum: string = bankNoString.substr(bankNoString.length - 1, 1)
  const first15Num: string = bankNoString.substr(0, bankNoString.length - 1)
  const newArr: number[] = []

  for (let i: number = first15Num.length - 1; i > -1; i--)
    newArr.push(Number.parseInt(first15Num.substr(i, 1)))

  const arrJiShu: number[] = []
  const arrJiShu2: number[] = []
  const arrOuShu: number[] = []

  for (let j = 0; j < newArr.length; j++) {
    if ((j + 1) % 2 === 1) {
      if (newArr[j] * 2 < 9)
        arrJiShu.push(newArr[j] * 2)

      else
        arrJiShu2.push(newArr[j] * 2)
    }
    else {
      arrOuShu.push(newArr[j])
    }
  }

  const jishu_child1: number[] = []
  const jishu_child2: number[] = []

  for (let h = 0; h < arrJiShu2.length; h++) {
    jishu_child1.push(arrJiShu2[h] % 10)
    jishu_child2.push(Math.floor(arrJiShu2[h] / 10))
  }

  let sumJiShu = 0
  let sumOuShu = 0
  let sumJiShuChild1 = 0
  let sumJiShuChild2 = 0
  let sumTotal = 0

  for (let m = 0; m < arrJiShu.length; m++)
    sumJiShu += arrJiShu[m]

  for (let n = 0; n < arrOuShu.length; n++)
    sumOuShu += arrOuShu[n]

  for (let p = 0; p < jishu_child1.length; p++) {
    sumJiShuChild1 += jishu_child1[p]
    sumJiShuChild2 += jishu_child2[p]
  }

  sumTotal = sumJiShu + sumOuShu + sumJiShuChild1 + sumJiShuChild2
  const k: number = sumTotal % 10 === 0 ? 10 : sumTotal % 10
  const luhm: number = 10 - k

  return lastNum === luhm.toString()
}

/**
 * 登录后跳转判断
 * 计算出当前router路径
 * 1.如果跳转的链接为登录页面或跳转的链接为空页面。则会重新跳转到首页
 * 2.都不满足返回跳转页面
 * @param type  'default' || 'wx'  //返回地址会做判断默认为default
 */

export function whetherNavigate(type = 'default') {
  const navigation = getCurrentPages()[getCurrentPages().length - (getCurrentPages().length)]
  if (getCurrentPages().length > 1) {
    if (navigation.route === 'pages/passport/login') {
      navigationToBack(type)
    }
    else {
      if (!navigation.route || navigation.route === 'undefined') {
        navigationToBack(type)
      }
      else {
        uni.navigateBack({
          delta: getCurrentPages().length,
        })
      }
    }
  }
  else {
    uni.switchTab({
      url: '/pages/tabbar/home/index',
    })
  }
}

/**
 * 将unix时间戳转换为指定格式
 * @param unix   时间戳【秒】
 * @param format 转换格式
 * @returns {*|string}
 */
export function unixToDate(unix: string | number, format: string | undefined) {
  if (!unix)
    return unix
  let _format = format || 'yyyy-MM-dd hh:mm:ss'
  const d = new Date(unix)
  const o: any = {
    'M+': d.getMonth() + 1,
    'd+': d.getDate(),
    'h+': d.getHours(),
    'm+': d.getMinutes(),
    's+': d.getSeconds(),
    'q+': Math.floor((d.getMonth() + 3) / 3),
    'S': d.getMilliseconds(),
  }
  if (/(y+)/.test(_format)) {
    _format = _format.replace(
      RegExp.$1,
      (`${d.getFullYear()}`).substr(4 - RegExp.$1.length),
    )
  }
  for (const k in o) {
    if (new RegExp(`(${k})`).test(_format)) {
      _format = _format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : (`00${o[k]}`).substr((`${o[k]}`).length),
      )
    }
  }
  return _format
}

/**
 * 将时间转unix时间戳
 * @param date
 * @returns {number} 【秒】
 */
export function dateToUnix(date: string | number) {
  let newStr = String(date).replace(/:/g, '-')
  newStr = newStr.replace(/ /g, '-')
  const arr = newStr.split('-')
  const datum = new Date(
    Date.UTC(
      Number(arr[0]),
      Number(arr[1]) - 1,
      Number(arr[2]),
      Number(arr[3]) - 8 || -8,
      Number(arr[4]) || 0,
      Number(arr[5]) || 0,
    ),
  )
  return datum.getTime() / 1000
}

/**
 * 货币格式化
 * @param price
 * @returns {string}
 */
export function formatPrice(price: string | number) {
  if (typeof price !== 'number')
    return price
  return String(Number(price).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

/**
 * 手机号隐私保护
 * 隐藏中间四位数字
 * @param mobile
 * @returns {*}
 */
export function secrecyMobile(mobile: string | number) {
  mobile = String(mobile)
  if (!/\d{11}/.test(mobile))
    return mobile

  return mobile.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3')
}

/**
 * 随机生成指定长度的字符串
 * @param length
 * @returns {string}
 */
export function randomString(length = 32) {
  const chars
    = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  const maxPos = chars.length
  let _string = ''
  for (let i = 0; i < length; i++)
    _string += chars.charAt(Math.floor(Math.random() * maxPos))

  return _string
}

function navigationToBack(type: string | undefined) {
  if (type === 'wx') {
    // console.log(getCurrentPages().length - 3)
    uni.navigateBack({
      delta: getCurrentPages().length,
    })
  }
  else {
    uni.switchTab({
      url: '/pages/tabbar/home/index',
    })
  }
}

/**
 * 计算当前时间到第二天0点的倒计时[秒]
 * @returns {number}
 */
export function theNextDayTime() {
  const nowDate = new Date()
  const time
    = new Date(
      nowDate.getFullYear(),
      nowDate.getMonth(),
      nowDate.getDate() + 1,
      0,
      0,
      0,
    ).getTime() - nowDate.getTime()
  return time / 1000
}
